<cfcomponent displayname="ReactorAdapter" extends="actionpacks.persistence.PersistenceService" output="false">

	<cffunction name="init" returntype="any" output="false" hint="Constructor">
		<cfargument name="resourceurl" required="true">
		<cfargument name="svnService" required="true">
		<cfset variables.instance.resourceUrl = arguments.resourceUrl>
		<cfset variables.instance.svnService = arguments.svnService />
		<cfreturn this />
	</cffunction>

	<cffunction name="setSvnService" access= "public" returntype="void" output="false">	
		<cfargument name="svnService" type= "any" required="true">	
		<cfset variables.svnService = arguments.svnService/>
	</cffunction>

	<cffunction name="getSvnService" access= "public" returntype="any" output="false">	
		<cfreturn variables.svnService />
	</cffunction>

	<cffunction name="GetFoldersAndFiles" access= "public" returntype="any" output="false">
		<cfargument name="resourceURL" default="/">
	  <cfset var foldersAndFiles = getSvnService().list(resourceURL)>
	  <cfquery name="getFiles" dbtype="query">
		  SELECT * FROM foldersAndFiles
		  WHERE lower(kind) LIKE 'file'
		</cfquery>
	  <cfquery name="getFolders" dbtype="query">
		  SELECT * FROM foldersAndFiles
		  WHERE lower(kind) LIKE 'dir'
		</cfquery>
<!---
 	  <cfdump var="#foldersAndFiles#"><cfabort >
 --->
	  <cfset daFile = '<?xml version="1.0" encoding="utf-8" ?>
		<Connector command="GetFoldersAndFiles" resourceType="File">
			<CurrentFolder path="#resourceURL#" url="#resourceURL#" />
			<Folders>'>
			<cfloop query="getFolders">
				<cfset daFile = daFile & '<Folder name="#listLast(name,'/')#" />'>
			</cfloop>
			<cfset daFile = daFile & '</Folders>'>
			<cfset daFile = daFile & '<Files>'>
			<cfloop query="getFiles">
				<cfset daFileName = xmlformat("#name#")>
				<cfset daFile = daFile & '<File name="#daFileName#" size="#size#" url="#url#" />'>
			</cfloop>
			<cfset daFile = daFile & '</Files>
		</Connector>'>
	  <cfreturn daFile>
  </cffunction>

	<cffunction name="GetFolders" access= "public" returntype="any" output="false">
		<cfargument name="resourceURL" default="/">
	  <cfset var foldersAndFiles = getSvnService().list(resourceURL)>
	  <cfquery name="getFolders" dbtype="query">
		  SELECT * FROM foldersAndFiles
		  WHERE lower(kind) LIKE 'dir'
		</cfquery>
<!---
 	  <cfdump var="#foldersAndFiles#"><cfabort >
 --->
	  <cfset daFile = '<?xml version="1.0" encoding="utf-8" ?>
		<Connector command="GetFoldersAndFiles" resourceType="File">
			<CurrentFolder path="#resourceURL#" url="#resourceURL#" />
			<Folders>'>
			<cfloop query="getFolders">
				<cfset daFile = daFile & '<Folder name="#listLast(name,'/')#" />'>
			</cfloop>
			<cfset daFile = daFile & '</Folders>
		</Connector>'>
	  <cfreturn daFile>
  </cffunction>

	<cffunction name="CreateFolder" access= "public" returntype="any" output="false">
		<cfargument name="currentFolder" default="/">
		<cfargument name="newFolderName" default="/">
		<cfargument name="commitMessage" default="/">
		<cfset var newFolder = arguments.currentFolder & arguments.newFolderName>
	  <cfset var daError = 0>
			<cftry>
		  <cfset newFolder = getSvnService().createDirectory(newFolder, arguments.commitMessage)>
			<!--- 	
			Possible Error Numbers are:
			o 0 : No Errors Found. The folder has been created.
			o 101 : Folder already exists.
			o 102 : Invalid folder name.
			o 103 : You have no permissions to create the folder.
			o 110 : Unknown error creating folder.
			 --->
<!---
 				<cfoutput>
					1#CFCATCH.Type#<br>
					2#CFCATCH.Message#<br>
					2#CFCATCH.detail#<br>
					3#daError#
					4#CFCATCH.ErrorCode#<br>
				</cfoutput><br>	
				<cfabort>
 --->
				<cfcatch>
					<cfif findNoCase('File already exists',cfcatch.message)>
						<cfset daError = 101>
					<cfelse>
						<cfdump var="#cfcatch#"><cfabort>
					</cfif>
				</cfcatch>
			</cftry>
		<cfset daFile = '<?xml version="1.0" encoding="utf-8" ?> 
		<Connector command="CreateFolder" resourceType="File">
			<CurrentFolder path="#currentFolder#" url="#CurrentFolder#" />'>
			<cfset daFile = daFile & '<Error number="#daError#" />'>
		<cfset daFile = daFile & '</Connector>'>
	  <cfreturn daFile>
	</cffunction>

	<cffunction name="CreateFile" access= "public" returntype="any" output="false">
		<cfargument name="currentFolder" default="/">
		<cfargument name="newFileName" default="/">
		<cfargument name="commitMessage" default="/">
		<cfset var newFolder = "">
	  <cfset var daError = 0>
	  <cfset var url = currentFolder>
			<cftry>
		  <cfset newFolder = getSvnService().addOrUpdateResource(url & newFileName,commitMessage,"Blank File")>
			<!--- 	
			Possible Error Numbers are:
			o 0 : No Errors Found. The folder has been created.
			o 101 : Folder already exists.
			o 102 : Invalid folder name.
			o 103 : You have no permissions to create the folder.
			o 110 : Unknown error creating folder.
			 --->
<!---
 				<cfoutput>
					1#CFCATCH.Type#<br>
					2#CFCATCH.Message#<br>
					2#CFCATCH.detail#<br>
					3#daError#
					4#CFCATCH.ErrorCode#<br>
				</cfoutput><br>	
				<cfabort>
 --->
				<cfcatch>
					<cfif findNoCase('File already exists',cfcatch.message)>
						<cfset daError = 101>
					</cfif>
				</cfcatch>
			</cftry>
		<cfset daFile = '<?xml version="1.0" encoding="utf-8" ?> 
		<Connector command="CreateFile" resourceType="File">
			<CurrentFolder path="#currentFolder#" url="#CurrentFolder#" />'>
			<cfset daFile = daFile & '<Error number="#daError#" />'>
		<cfset daFile = daFile & '</Connector>'>
	  <cfreturn daFile>
	</cffunction>

	<cffunction name="ExportResource" access= "public" returntype="any" output="false">
		<cfargument name="currentFolder" default="/">
		<cfset var newFolder = arguments.currentFolder>
	  <cfset var daError = 0>
			<cftry>
		  <cfset newFolder = getSvnService().export(newFolder)>
			<!--- 	
			Possible Error Numbers are:
			o 0 : No Errors Found. The folder has been created.
			o 101 : Folder already exists.
			o 102 : Invalid folder name.
			o 103 : You have no permissions to create the folder.
			o 110 : Unknown error creating folder.
			 --->
<!---
 				<cfoutput>
					1#CFCATCH.Type#<br>
					2#CFCATCH.Message#<br>
					2#CFCATCH.detail#<br>
					3#daError#
					4#CFCATCH.ErrorCode#<br>
				</cfoutput><br>	
				<cfabort>
 --->
				<cfcatch>
					<cfif findNoCase('File already exists',cfcatch.message)>
						<cfset daError = 101>
					<cfelse>
						<cfdump var="#cfcatch#"><cfabort>
					</cfif>
				</cfcatch>
			</cftry>
		<cfset daFile = '<?xml version="1.0" encoding="utf-8" ?> 
		<Connector command="ExportResource" resourceType="File">
			<CurrentFolder path="#currentFolder#" url="#CurrentFolder#" />'>
			<cfset daFile = daFile & '<Error number="#daError#" />'>
		<cfset daFile = daFile & '</Connector>'>
	  <cfreturn daFile>
	</cffunction>


	<cffunction name="persist" returntype="any" access="public" output="false">
		<cfargument name="persistObject" type="any" required="true" hint="object to persist"/>
		<cfargument name="useTransaction" type="boolean" default="false" />
		<cfscript>
			var recordOb = getRecord();
			var popRec = recordOb.populate(persistObject);
			var record = recordOb.load();
			var errors = "";
			var dict = "";
			var errorCollection = createObject("component", "ModelGlue.Util.ValidationErrorCollection").init();
			var x = 0;
			record.validate();

			if(NOT record.hasErrors()) {
				record.save(arguments.useTransaction);
			} 
			else {
				errors = record._getErrorCollection().getErrors();
				dict = record._getDictionary();
				for(x=1; x lte arrayLen(errors); x = x+1) {
					errorCollection.addError(listGetAt(errors[x], 2, "."), dict.getValue(errors[x]));
				}
			}
//			dumpvar(record._getTo());
			return errorCollection.getErrors();
		</cfscript>
	</cffunction>


	<cffunction name="persisted" returntype="any" output="false" hint="Create Project Object">
		<cfargument name="persistedObject" type="any" required="true" hint="object to persist"/>
		<cfargument name="useTransaction" type="boolean" default="false" />
		<cfscript>
			var record = getRecord();
			record.populate(persistedObject);
			record.load();
			populate(arguments.persistedObject,record);
			return persistedObject;
		</cfscript>
	</cffunction>


</cfcomponent>